package com.xinfan.mhl.service;

import com.xinfan.mhl.dao.BillDAO;
import com.xinfan.mhl.dao.MultiBeanDAO;
import com.xinfan.mhl.domain.Bill;
import com.xinfan.mhl.domain.Menu;
import com.xinfan.mhl.domain.MultiBean;

import java.util.List;
import java.util.UUID;

/**
 * @Author zhangxin
 * @Date 2022/6/24 9:09
 * @Version 1.0
 */
public class BillService {
    private BillDAO billDAO = new BillDAO();
    private DiningTableService diningTableService = new DiningTableService();
    private MenuService menuService = new MenuService();

    /**
     * @Description 点菜创建订单
     * @Param [tableId, menuId, nums]
     * @return boolean
     * @Author zhangxin
     * @Date 2022/6/24 13:45
     * @Version 1.0
     **/
    public boolean createBill(int tableId,int menuId,int nums) {
        UUID uuid = UUID.randomUUID();

        int update = billDAO.update("insert into bill values(null,?,?,?,?,?,now(),'未支付')", uuid.toString(), menuId, nums, menuService.getMenuById(menuId).getPrice()*nums, tableId);

        //订单生成修改餐桌状态---就餐中
        if (update > 0) {
            return diningTableService.useTable(tableId);
        } else {
            return false;
        }
    }

    /**
     * @Description 显示所有账单信息
     * @Param []
     * @return java.util.List<com.xinfan.mhl.domain.Bill>
     * @Author zhangxin
     * @Date 2022/6/24 16:28
     * @Version 1.0
     **/
    public List<Bill> getBills() {
        return billDAO.selectMany("select * from bill;", Bill.class);
    }

    /**
     * @Description 支付账单
     * @Param [tableId, payMode]
     * @return boolean
     * @Author zhangxin
     * @Date 2022/6/24 17:10
     * @Version 1.0
     **/
    public boolean payBill(int tableId,String payMode) {
        int update = billDAO.update("update bill set state = ? where diningTableId = ?", payMode, tableId);
        if (update > 0) {
            //支付成功 修改餐桌状态
            return diningTableService.cleanTable(tableId);
        } else {
            return false;
        }
    }

}
